package com.app.controller.miniprogram;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class UploadPDFController {
    @PostMapping(value = "/uploadPdfFile")
    public void uploadPdf(MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("File is empty");
        }
        if (!file.getOriginalFilename().endsWith(".pdf")) {
            throw new RuntimeException("File is not a PDF");
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get("/uploads/" + file.getOriginalFilename());
            Files.write(path,bytes);
        } catch (IOException e) {
            throw new RuntimeException("Error uploading file", e);
        }
    }

}
